package com.dalei.vdserver.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.dalei.vdserver.model.DeviceInformation;

public class DeviceInformationDaoImpl extends JdbcDaoSupport implements
		DeviceInformationDao {

	@Override
	@SuppressWarnings("unchecked")
	public List<DeviceInformation> getDevices() {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_device_information";
		List<DeviceInformation> devices = null;
		try {
			devices = getJdbcTemplate().query(sql, new RowMapper() {

				@Override
				public Object mapRow(ResultSet rs, int rowNum)
						throws SQLException {
					// TODO Auto-generated method stub
					DeviceInformation device = new DeviceInformation();
					device.setId(rs.getInt("_id"));
					device.setModel(rs.getString("_model"));
					device.setImei(rs.getString("_imei"));
					device.setImeiSv(rs.getString("_imeiSv"));
					device.setSerialNo(rs.getString("_serialNo"));
					device.setWlanMac(rs.getString("_wlanMac"));
					device.setOsVersion(rs.getString("_osVersion"));
					Timestamp timestamp = rs
							.getTimestamp("_registrationTime");
					Date date = new Date(timestamp.getTime());
					device.setRegistrationTime(date);
					return device;
				}
			});
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return devices;
	}

	@Override
	public DeviceInformation getDeviceById(int id) {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_device_information WHERE _id=?";
		DeviceInformation info = null;
		try {
			info = (DeviceInformation) getJdbcTemplate().queryForObject(sql,
					new Object[] { id }, new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							DeviceInformation i = new DeviceInformation();
							i.setId(rs.getInt("_id"));
							i.setModel(rs.getString("_model"));
							i.setImei(rs.getString("_imei"));
							i.setImeiSv(rs.getString("_imeiSv"));
							i.setSerialNo(rs.getString("_serialNo"));
							i.setWlanMac(rs.getString("_wlanMac"));
							i.setOsVersion(rs.getString("_osVersion"));
							Timestamp timestamp = rs
									.getTimestamp("_registrationTime");
							Date date = new Date(timestamp.getTime());
							i.setRegistrationTime(date);
							return i;
						}
					});
		} catch (IncorrectResultSizeDataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return info;
	}

	@Override
	public int addDevice(final DeviceInformation device) {
		// TODO Auto-generated method stub
		final String sql = "INSERT INTO vds_device_information (_model, _imei, _imeiSv, _serialNo, _wlanMac, _osVersion) "
				+ "VALUES (?, ?, ?, ?, ?, ?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		int id = 0;
		try {
			getJdbcTemplate().update(new PreparedStatementCreator() {

				@Override
				public PreparedStatement createPreparedStatement(Connection con)
						throws SQLException {
					// TODO Auto-generated method stub
					PreparedStatement ps = con.prepareStatement(sql,
							Statement.RETURN_GENERATED_KEYS);
					ps.setString(1, device.getModel());
					ps.setString(2, device.getImei());
					ps.setString(3, device.getImeiSv());
					ps.setString(4, device.getSerialNo());
					ps.setString(5, device.getWlanMac());
					ps.setString(6, device.getOsVersion());
					return ps;
				}
			}, keyHolder);
			id = keyHolder.getKey().intValue();
		} catch (InvalidDataAccessApiUsageException e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		} catch (DataRetrievalFailureException e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
		return id;
	}
}
